package com.conop.gradient;

public class HalfspaceSet implements ConvexSet 
{

	private NDPoint _coeff;
	private double _intersect;
	
	public HalfspaceSet(NDPoint coeff, double intersect)
	{
		_coeff = new NDPoint(coeff);
		_intersect = intersect;
	}
	
	public HalfspaceSet(double[] arr, double intersect)
	{
		this(new NDPoint(arr), intersect);
	}
	
	@Override
	public boolean isFeasible(NDPoint pnt) 
	{
		return (pnt.DotProduct(_coeff) <= _intersect);
	}

	@Override
	public NDPoint Projection(NDPoint pnt) 
	{
		return (pnt.Substraction(
				_coeff.Multiply(Math.max(0, _coeff.DotProduct(pnt) - _intersect) / Math.pow(_coeff.Norm(), 2))));
	}

	@Override
	public String toString()
	{
		StringBuilder sb = new StringBuilder("Halfspace set:\n");
		sb.append("\t").append(_coeff.toString()).append(" <= ").append(_intersect);
		
		return sb.toString();
	}
}
